/***********************************************
 * Title:       CertificateAction.java
 * Description: CertificateAction.java
 * Author:      Niyuzhe
 * Create Date: 2010-3-5
 * CopyRight:   CopyRight(c)@2009
 * Company:     TJUSCS
 * Version:     1.0
 ***********************************************
 */
package edu.tju.student.certificate;

import java.sql.SQLException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;
import org.infohazard.maverick.flow.ControllerContext;



import com.younker.waf.maverick.DispatchCtl;

/**
 * @author Niyuzhe
 *
 */
public class CertificateAction extends DispatchCtl
{
        private Logger log = Logger.getLogger(this.getClass());
        //view names
        private final static String V_CERTICICATE = "certicicate";

        //parameters
        private String certificateName;
        private long certificateId;
        private Certificate certificate; 
        private CertificateLevel certificateLevel;
        private long certificateLevelId;
        private String certificateLevelName;
        private long studentId;
        
        /* (non-Javadoc)
         * @see org.infohazard.maverick.ctl.FormBeanUser#makeFormBean(org.infohazard.maverick.flow.ControllerContext)
         */
        @Override
        protected Object makeFormBean(ControllerContext cctx)
        {
                return new CertificateAction();
        }
        
        
        public String displayCertificate( Object beans, ControllerContext ctx)
        {
                CertificateAction bean = (CertificateAction)beans;
                long id = bean.getCertificateId();
                Certificate certificate ;
                try
                {
                        certificate = (Certificate) sqlMap.queryForObject("getCertificateById", id);
                        bean.setCertificate(certificate);
                } catch (SQLException e)
                {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }
                
                
                
                return V_CERTICICATE;
        }
        
        public String displayCertificateLevel( Object beans, ControllerContext ctx )
        {
                CertificateAction bean = (CertificateAction)beans;
                long certificateId = bean.getCertificateId();
                long certificateLevelId = bean.getCertificateLevelId();
                
                try
                {
                        Certificate certificate = (Certificate) sqlMap.queryForObject("getCertificateById", certificateId);
                        bean.setCertificate(certificate);
                        CertificateLevel certificateLevel = (CertificateLevel) sqlMap.queryForObject("getCertificateLevelById", certificateLevelId);
                        bean.setCertificateLevel(certificateLevel);
                } catch (SQLException e)
                {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }
                
                return V_CERTICICATE;
        }
        
        public String saveOrUpdateCertificate( Object beans, ControllerContext ctx )
        {
                
                CertificateAction bean = (CertificateAction)beans;
                Certificate certificate = new Certificate();
                certificate.setId(bean.getCertificateId());
                certificate.setName(bean.getCertificateName());
                
                if( certificate.getId() == 0 )
                {
                        try
                        {
                                Long id = (Long)sqlMap.insert("insertCertificate", certificate);
                                certificate.setId(id);
                                ctx.getRequest().setAttribute(RET_CODE, RC_SUCCESS);
                        } catch (SQLException e)
                        {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                        }
                }else
                {
                        try
                        {
                                sqlMap.update("updateCertificate", certificate);
                                ctx.getRequest().setAttribute(RET_CODE, RC_SUCCESS);
                        } catch (SQLException e)
                        {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                        }
                }
                
                bean.setCertificate(certificate);
                
                return V_CERTICICATE;
        }
        
        
        public String saveOrUpdateCertificateLevel( Object beans, ControllerContext ctx )
        {
                
                CertificateAction bean = (CertificateAction)beans;
                Certificate certificate = new Certificate();
                certificate.setId(bean.getCertificateId());
                certificate.setName(bean.getCertificateName());                
                bean.setCertificate(certificate);
                
                CertificateLevel certificateLevel = new CertificateLevel();
                certificateLevel.setId(bean.getCertificateLevelId());
                certificateLevel.setName(bean.getCertificateLevelName());
                certificateLevel.setCertificateId(bean.getCertificateId());

                log.debug("++++++++++++"+ certificateLevel);
                if( certificateLevel.getId() == 0 )
                {
                        try
                        {
                                Long id = (Long)sqlMap.insert("insertCertificateLevel", certificateLevel);
                                certificateLevel.setId(id);
                                ctx.getRequest().setAttribute(RET_CODE, RC_SUCCESS);
                        } catch (SQLException e)
                        {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                        }
                }else
                {
                        try
                        {
                                sqlMap.update("updateCertificateLevel", certificateLevel);
                                ctx.getRequest().setAttribute(RET_CODE, RC_SUCCESS);
                        } catch (SQLException e)
                        {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                        }
                }
                
                return V_CERTICICATE;
        }
        
        public String gain( Object beans, ControllerContext ctx)
        {
                CertificateAction bean = (CertificateAction)beans;
                Map<String, Object> certificateStudent = new HashMap<String, Object>();
                certificateStudent.put("certificateLevelId", bean.getCertificateLevelId());
                certificateStudent.put("studentId", bean.getStudentId());
                certificateStudent.put("gainTime", new Date());
                try
                {
                        sqlMap.insert("insertCertificateStudent", certificateStudent);
                        ctx.getRequest().setAttribute(RET_CODE, RC_SUCCESS);
                } catch (SQLException e)
                {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }
                return V_CERTICICATE;
        }

        /**
         * @return the certificateName
         */
        public String getCertificateName()
        {
                return certificateName;
        }

        /**
         * @param certificateName the certificateName to set
         */
        public void setCertificateName(String certificateName)
        {
                this.certificateName = certificateName;
        }

        /**
         * @return the certificateId
         */
        public long getCertificateId()
        {
                return certificateId;
        }

        /**
         * @param certificateId the certificateId to set
         */
        public void setCertificateId(long certificateId)
        {
                this.certificateId = certificateId;
        }

        /**
         * @return the certificate
         */
        public Certificate getCertificate()
        {
                return certificate;
        }

        /**
         * @param certificate the certificate to set
         */
        public void setCertificate(Certificate certificate)
        {
                this.certificate = certificate;
        }


        /**
         * @return the certificateLevel
         */
        public CertificateLevel getCertificateLevel()
        {
                return certificateLevel;
        }


        /**
         * @param certificateLevel the certificateLevel to set
         */
        public void setCertificateLevel(CertificateLevel certificateLevel)
        {
                this.certificateLevel = certificateLevel;
        }


        /**
         * @return the certificateLevelId
         */
        public long getCertificateLevelId()
        {
                return certificateLevelId;
        }


        /**
         * @param certificateLevelId the certificateLevelId to set
         */
        public void setCertificateLevelId(long certificateLevelId)
        {
                this.certificateLevelId = certificateLevelId;
        }


        /**
         * @return the certificateLevelName
         */
        public String getCertificateLevelName()
        {
                return certificateLevelName;
        }


        /**
         * @param certificateLevelName the certificateLevelName to set
         */
        public void setCertificateLevelName(String certificateLevelName)
        {
                this.certificateLevelName = certificateLevelName;
        }


        /**
         * @param studentId the studentId to set
         */
        public void setStudentId(long studentId)
        {
                this.studentId = studentId;
        }


        /**
         * @return the studentId
         */
        public long getStudentId()
        {
                return studentId;
        }

}
